<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">M B T I 综合管理系统</div>
            <el-form :model="param" :rules="loginRules" class="ms-content">
                <el-form-item prop="Username">
                    <el-input clearable prefix-icon="user" placeholder="username" v-model="param.Username">
                    </el-input>
                </el-form-item>
                <el-form-item prop="Password">
                    <el-input clearable prefix-icon="lock" type="password" placeholder="password"
                        v-model="param.Password" @keyup.enter="submitForm()">
                    </el-input>
                </el-form-item>

                <el-row :gutter="25" type="flex" justify="center" class="ms-radios">
                    <el-radio-group v-model="identify">
                        <el-radio label="true">学生</el-radio>
                        <el-radio label="false">管理员</el-radio>
                    </el-radio-group>
                </el-row>

                <el-row :gutter="15" type="flex" justify="center">
                    <el-form-item class="btns">
                        <el-button type="primary" @click="submitForm()">登录</el-button>
                        <el-button @click="resetForm()">重置</el-button>
                    </el-form-item>
                </el-row>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data: function () {
        return {
            param: {
                Username: "",
                Password: "",
            },
            loginRules: {
                Username: [
                    {
                        required: true,
                        message: "请输入用户ID",
                        trigger: "blur"
                    },
                ],
                Password: [
                    {
                        required: true,
                        message: "请输入密码",
                        trigger: "blur"
                    }
                ],
            },
            identify: "true",
        };
    },
    methods: {
        async submitForm() {
            if (this.param.Password == "" || this.param.Username == "") {
                this.$message.error("请输入 ID 和密码");
                return false;
            }
            if (this.identify === "true") {
                console.log(this.param);
                const { data: res } = await this.$http.post(
                    "api/login/student",
                    this.param
                );
                if (res.Code == 200) {
                    this.$message.success("登录成功");
                    window.sessionStorage.setItem("Name", res.Data["Name"]);
                    window.sessionStorage.setItem("Sex", res.Data["Sex"]);
                    window.sessionStorage.setItem("Age", res.Data["Age"]);
                    window.sessionStorage.setItem("No", res.Data["No"]);
                    window.sessionStorage.setItem("Token", res.Data["Token"]);
                    // localStorage.setItem("ms_username", this.param.username);
                    this.$router.push("/stuMain");
                } else {
                    this.$message.error("用户名或密码错误");
                }
            } else {
                const { data: res } = await this.$http.post(
                    "api/login/admin",
                    this.param
                );
                if (res.Code == 200) {
                    this.$message.success("登录成功");
                    window.sessionStorage.setItem("username", res.Data["Username"]);
                    window.sessionStorage.setItem("identify", "管理员");
                    window.sessionStorage.setItem("Token", res.Data["Token"]);
                    this.$router.push("/adMain");
                } else {
                    this.$message.error("用户名或密码错误");
                }
            }
        },
        resetForm() {
            this.param.Username = "";
            this.param.Password = "";
            this.identify = "true";
        }
    },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../assets/bg-login.png);
    background-size: 100%;
}

.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}

.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.15);
    overflow: hidden;
}

.ms-content {
    padding: 30px 30px;
}

.el-radio {
    color: rgb(0, 0, 0);
}
</style>